from urllib.request import Request
from urllib.request import urlopen
from bs4 import BeautifulSoup
from urllib.error import URLError

def fans_catch():
    try:
        url='https://m.weibo.cn/p/second?containerid=1005051800847837_-_FANS'
        head = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
                'Accept-Language': 'zh-CN,zh;q=0.8', 'Connection': 'keep-alive',
                'Host': 'm.weibo.cn',
                'Referer': 'https://m.weibo.cn/',
                'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36'}
        cook_file=open('../file/cookies.txt','r')
        head['Cookie']=cook_file.readline()
        print(head)
        request=Request(url=url,headers=head)
        respon=urlopen(request).read().decode('utf-8')
        soup=BeautifulSoup(respon,'html.parser')
        print(soup.prettify())
    except URLError as e:
        if hasattr(e,'code'):
            print(e.code)
        if hasattr(e,'reason'):
            print(e.reason)

if __name__=='__main__':
    fans_catch()